package com.alibaba.citrus.cr.sales.plan.facade.service.salesplan.converter;

import com.alibaba.citrus.cr.sales.plan.functions.ability.model.SalesPlanStatAbilityRequest;
import com.alibaba.citrus.cr.sales.plan.functions.ability.model.SalesPlanStatRequest;
import com.alibaba.citrus.cr.sales.plan.sdo.SalesPlanLineSDO;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.EditSalesPlanLineRequest;

import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wangyibing
 */
public class SalesPlanStatRequestConverter {

    public static SalesPlanStatAbilityRequest deletePlanCustomerConverter(List<SalesPlanLineSDO> sdos) {
        List<SalesPlanStatRequest> salesPlanStatRequests = sdos.stream().map(SalesPlanStatRequestConverter::getSalesPlanStatDecreaseRequest).collect(Collectors.toList());
        SalesPlanStatAbilityRequest statAbilityRequest = new SalesPlanStatAbilityRequest();
        statAbilityRequest.setSalesPlanStatRequestList(salesPlanStatRequests);
        statAbilityRequest.setStatPlanCustomer(false);
        statAbilityRequest.setStatPlanScItem(true);
        statAbilityRequest.setIsAdjust(false);
        return statAbilityRequest;
    }

    public static SalesPlanStatAbilityRequest deletePlanScItemConverter(List<SalesPlanLineSDO> sdos) {
        List<SalesPlanStatRequest> salesPlanStatRequests = sdos.stream().map(SalesPlanStatRequestConverter::getSalesPlanStatDecreaseRequest).collect(Collectors.toList());
        SalesPlanStatAbilityRequest statAbilityRequest = new SalesPlanStatAbilityRequest();
        statAbilityRequest.setSalesPlanStatRequestList(salesPlanStatRequests);
        statAbilityRequest.setStatPlanCustomer(true);
        statAbilityRequest.setStatPlanScItem(false);
        statAbilityRequest.setIsAdjust(false);
        return statAbilityRequest;
    }

    public static SalesPlanStatAbilityRequest submitSalesPlanLineConverter(SalesPlanLineSDO sdo) {
        SalesPlanStatAbilityRequest statAbilityRequest = new SalesPlanStatAbilityRequest();
        SalesPlanStatRequest salesPlanStatAbilityRequest = getSalesPlanStatIncreaseRequest(sdo);
        statAbilityRequest.setSalesPlanStatRequestList(Lists.newArrayList(salesPlanStatAbilityRequest));
        statAbilityRequest.setStatPlanCustomer(true);
        statAbilityRequest.setStatPlanScItem(true);
        statAbilityRequest.setIsAdjust(false);
        return statAbilityRequest;
    }

    public static SalesPlanStatAbilityRequest deleteSalesPlanLineConverter(SalesPlanLineSDO sdo) {
        SalesPlanStatAbilityRequest statAbilityRequest = new SalesPlanStatAbilityRequest();
        SalesPlanStatRequest salesPlanStatAbilityRequest = getSalesPlanStatDecreaseRequest(sdo);
        statAbilityRequest.setSalesPlanStatRequestList(Lists.newArrayList(salesPlanStatAbilityRequest));
        statAbilityRequest.setStatPlanCustomer(true);
        statAbilityRequest.setStatPlanScItem(true);
        statAbilityRequest.setIsAdjust(false);
        return statAbilityRequest;
    }

    public static SalesPlanStatAbilityRequest editSalesPlanLineConverter(EditSalesPlanLineRequest editSalesPlanRequest, SalesPlanLineSDO origin) {
        SalesPlanStatAbilityRequest statAbilityRequest = new SalesPlanStatAbilityRequest();
        SalesPlanStatRequest salesPlanStatAbilityRequest = getSalesPlanStatDifferenceRequest(editSalesPlanRequest, origin);
        //编辑已完成填写的计划明细：月合同量和月合同金额差为0
        salesPlanStatAbilityRequest.setMonthContractCount(0);
        salesPlanStatAbilityRequest.setMonthContractMoney(0L);
        salesPlanStatAbilityRequest.setM1RollingPlanCount(0);
        salesPlanStatAbilityRequest.setM2RollingPlanCount(0);
        salesPlanStatAbilityRequest.setM3RollingPlanCount(0);
        statAbilityRequest.setSalesPlanStatRequestList(Lists.newArrayList(salesPlanStatAbilityRequest));
        statAbilityRequest.setStatPlanCustomer(true);
        statAbilityRequest.setStatPlanScItem(true);
        statAbilityRequest.setIsAdjust(false);
        return statAbilityRequest;
    }

    private static SalesPlanStatRequest getSalesPlanStatDifferenceRequest(EditSalesPlanLineRequest editSalesPlanRequest, SalesPlanLineSDO originSdo) {
        Integer editFirstCount = editSalesPlanRequest.getMonthFirstCount();
        Integer editMiddleCount = editSalesPlanRequest.getMonthMiddleCount();
        Integer editLastCount = editSalesPlanRequest.getMonthLastCount();
        // 判空
        editFirstCount = Objects.nonNull(editFirstCount) ? editFirstCount : 0;
        editMiddleCount = Objects.nonNull(editMiddleCount) ? editMiddleCount : 0;
        editLastCount = Objects.nonNull(editLastCount) ? editLastCount : 0;
        Integer monthFirstCount = Objects.nonNull(originSdo.getMonthFirstCount()) ? originSdo.getMonthFirstCount() : 0;
        Integer monthMiddleCount = Objects.nonNull(originSdo.getMonthMiddleCount()) ? originSdo.getMonthMiddleCount() : 0;
        Integer monthLastCount = Objects.nonNull(originSdo.getMonthLastCount()) ? originSdo.getMonthLastCount() : 0;
        // 计算差值
        SalesPlanStatRequest salesPlanStatRequest = getSalesPlanStatAbilityRequest(originSdo);
        salesPlanStatRequest.setMonthFirstCount(editFirstCount - monthFirstCount);
        salesPlanStatRequest.setMonthMiddleCount(editMiddleCount - monthMiddleCount);
        salesPlanStatRequest.setMonthLastCount(editLastCount - monthLastCount);
        salesPlanStatRequest.setMonthCount(salesPlanStatRequest.getMonthFirstCount() + salesPlanStatRequest.getMonthMiddleCount() + salesPlanStatRequest.getMonthLastCount());

        Long costInsuranceFreight = originSdo.getCostInsuranceFreight();
        costInsuranceFreight = Objects.nonNull(costInsuranceFreight) ? costInsuranceFreight : 0L;
        Long editMoney = salesPlanStatRequest.getMonthCount() * costInsuranceFreight;
        salesPlanStatRequest.setMonthMoney(editMoney);
        return salesPlanStatRequest;
    }

    @NotNull
    private static SalesPlanStatRequest getSalesPlanStatAbilityRequest(SalesPlanLineSDO sdo) {
        SalesPlanStatRequest salesPlanStatRequest = new SalesPlanStatRequest();
        salesPlanStatRequest.setPlanId(sdo.getPlanId());
    /*    if (Objects.nonNull(sdo.getIsAdjust()) && sdo.getIsAdjust() == 1) {
            salesPlanStatRequest.setAdjustId(sdo.getModelId());
        }*/
        salesPlanStatRequest.setDetailId(sdo.getId().toString());
        salesPlanStatRequest.setBasicUnit(sdo.getBasicUnit());
        salesPlanStatRequest.setChannelCode(sdo.getChannelCode());
        salesPlanStatRequest.setCostInsuranceFreight(sdo.getCostInsuranceFreight());
        salesPlanStatRequest.setCustomerAddress(sdo.getCustomerAddress());
        salesPlanStatRequest.setCustomerCode(sdo.getCustomerCode());
        salesPlanStatRequest.setEstimatedDiscountPrice(sdo.getEstimatedDiscountPrice());
        salesPlanStatRequest.setOrgBusinessUnitCode(sdo.getOrgBusinessUnitCode());
        salesPlanStatRequest.setOrgSalesChannelCode(sdo.getOrgSalesChannelCode());
        salesPlanStatRequest.setOrgSalesDepartmentCode(sdo.getOrgSalesDepartmentCode());
        salesPlanStatRequest.setManageEmployeeId(sdo.getManageEmployeeId());
        if (Objects.nonNull(sdo.getSalesPlanLineFeatures())) {
            salesPlanStatRequest.setScItemName(sdo.getSalesPlanLineFeatures().getScItemName());
            //salesPlanStatRequest.setBondType(sdo.getSalesPlanLineFeatures().getBoundType());
            salesPlanStatRequest.setCustomerName(sdo.getSalesPlanLineFeatures().getCustomerName());
            salesPlanStatRequest.setCustomerAddressName(sdo.getSalesPlanLineFeatures().getAddressDetails());
        }
        salesPlanStatRequest.setScItemLeafCategoryId(sdo.getScItemLeafCategoryId());
        salesPlanStatRequest.setScItemRootCategoryId(sdo.getScItemRootCategoryId());
        salesPlanStatRequest.setOutboundPrice(sdo.getOutboundPrice());
        salesPlanStatRequest.setScItemId(sdo.getScItemId());
        salesPlanStatRequest.setOrgSalesOrganizationCode(sdo.getOrgSalesOrganizationCode());
        salesPlanStatRequest.setScItemGroup(sdo.getScItemGroup());

        return salesPlanStatRequest;
    }

    private static SalesPlanStatRequest getSalesPlanStatDecreaseRequest(SalesPlanLineSDO sdo) {
        SalesPlanStatRequest salesPlanStatRequest = getSalesPlanStatAbilityRequest(sdo);
        salesPlanStatRequest.setMonthContractCount(-(Objects.nonNull(sdo.getMonthContractCount()) ? sdo.getMonthContractCount() : 0));
        salesPlanStatRequest.setMonthContractMoney(-(Objects.nonNull(sdo.getMonthContractMoney()) ? sdo.getMonthContractMoney() : 0));
        salesPlanStatRequest.setMonthMoney(-(Objects.nonNull(sdo.getMonthMoney()) ? sdo.getMonthMoney() : 0));
        salesPlanStatRequest.setMonthFirstCount(-(Objects.nonNull(sdo.getMonthFirstCount()) ? sdo.getMonthFirstCount() : 0));
        salesPlanStatRequest.setMonthMiddleCount(-(Objects.nonNull(sdo.getMonthMiddleCount()) ? sdo.getMonthMiddleCount() : 0));
        salesPlanStatRequest.setMonthLastCount(-(Objects.nonNull(sdo.getMonthLastCount()) ? sdo.getMonthLastCount() : 0));
        salesPlanStatRequest.setM1RollingPlanCount(-(Objects.nonNull(sdo.getM1RollingPlanCount()) ? sdo.getM1RollingPlanCount() : 0));
        salesPlanStatRequest.setM2RollingPlanCount(-(Objects.nonNull(sdo.getM2RollingPlanCount()) ? sdo.getM2RollingPlanCount() : 0));
        salesPlanStatRequest.setM3RollingPlanCount(-(Objects.nonNull(sdo.getM3RollingPlanCount()) ? sdo.getM3RollingPlanCount() : 0));
        salesPlanStatRequest.setMonthCount(salesPlanStatRequest.getMonthFirstCount() + salesPlanStatRequest.getMonthMiddleCount() + salesPlanStatRequest.getMonthLastCount());
        return salesPlanStatRequest;
    }

    private static SalesPlanStatRequest getSalesPlanStatIncreaseRequest(SalesPlanLineSDO sdo) {
        SalesPlanStatRequest salesPlanStatRequest = getSalesPlanStatAbilityRequest(sdo);
        salesPlanStatRequest.setMonthContractCount(sdo.getMonthContractCount());
        salesPlanStatRequest.setMonthContractMoney(sdo.getMonthContractMoney());
        // 失焦保存的时候已经计算
        salesPlanStatRequest.setMonthMoney(sdo.getMonthMoney());
        salesPlanStatRequest.setMonthFirstCount(sdo.getMonthFirstCount());
        salesPlanStatRequest.setMonthMiddleCount(sdo.getMonthMiddleCount());
        salesPlanStatRequest.setMonthLastCount(sdo.getMonthLastCount());
        salesPlanStatRequest.setM1RollingPlanCount(sdo.getM1RollingPlanCount());
        salesPlanStatRequest.setM2RollingPlanCount(sdo.getM2RollingPlanCount());
        salesPlanStatRequest.setM3RollingPlanCount(sdo.getM3RollingPlanCount());

        salesPlanStatRequest.setMonthCount(sdo.getMonthFirstCount() + sdo.getMonthMiddleCount() + sdo.getMonthLastCount());
        return salesPlanStatRequest;
    }

    public static SalesPlanStatAbilityRequest submitSalesPlanLineListConverter(List<SalesPlanLineSDO> submitStatList) {
        List<SalesPlanStatRequest> planStatRequests = submitStatList.stream().map(SalesPlanStatRequestConverter::getSalesPlanStatIncreaseRequest).collect(Collectors.toList());
        SalesPlanStatAbilityRequest statAbilityRequest = new SalesPlanStatAbilityRequest();
        statAbilityRequest.setSalesPlanStatRequestList(planStatRequests);
        statAbilityRequest.setStatPlanScItem(true);
        statAbilityRequest.setStatPlanCustomer(true);
        statAbilityRequest.setIsAdjust(false);
        return statAbilityRequest;
    }
}
